From 22e7ede6c885e30a8c083dfe2cb76fffe42f9fb7 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 9 Mar 2015 12:07:35 -0700 Subject: [PATCH] Fix all tests from the update to rust master --- Cargo.lock | 18 +++++++++++ Cargo.toml | 23 +++++--------- src/bin/cargo.rs | 41 +++++++++++++++++-------- tests/support/paths.rs | 26 ++++++++++++++-- tests/support/registry.rs | 4 +-- tests/test_cargo_build_auth.rs | 50 +++++++++++-------------------- tests/test_cargo_compile.rs | 3 +- tests/test_cargo_cross_compile.rs | 8 ++--- tests/test_cargo_new.rs | 3 +- tests/test_cargo_registry.rs | 4 +-- tests/tests.rs | 5 ++-- 11 files changed, 110 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5cef0e23d..1285b0254 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,6 +18,7 @@ dependencies = [ "rustc-serialize 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "semver 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", "tar 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)", + "tempdir 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)", "term 0.1.13 (registry+https://github.com/rust-lang/crates.io-index)", "threadpool 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "time 0.1.19 (registry+https://github.com/rust-lang/crates.io-index)", @@ -218,6 +219,15 @@ name = "pnacl-build-helper" version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rand" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "libc 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "log 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "regex" version = "0.1.18" @@ -246,6 +256,14 @@ name = "tar" version = "0.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "tempdir" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "rand 0.1.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "term" version = "0.1.13" diff --git a/Cargo.toml b/Cargo.toml index 012daed8e..8e9ae24f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,20 +28,16 @@ term = "0.1.13" regex = "0.1.18" threadpool = "0.1.1" libc = "0.1.2" +registry = { path = "src/registry" } -[target.i686-pc-windows-gnu.dependencies] -winapi = "0.1" -advapi32-sys = "*" +[target.i686-pc-windows-gnu] +dependencies = { winapi = "0.1", advapi32-sys = "*" } +[target.x86_64-pc-windows-gnu] +dependencies = { winapi = "0.1", advapi32-sys = "*" } -[target.x86_64-pc-windows-gnu.dependencies] -winapi = "0.1" -advapi32-sys = "*" - -[dev-dependencies.hamcrest] -git = "https://github.com/carllerche/hamcrest-rust.git" - -[dependencies.registry] -path = "src/registry" +[dev-dependencies] +tempdir = "0.3" +hamcrest = { git = "https://github.com/carllerche/hamcrest-rust.git" } [[bin]] name = "cargo" @@ -50,9 +46,6 @@ doc = false [[test]] name = "tests" -[[bench]] -name = "tests" -path = "tests/tests.rs" [[test]] name = "resolve" diff --git a/src/bin/cargo.rs b/src/bin/cargo.rs index 7707762cd..733d0dd1c 100644 --- a/src/bin/cargo.rs +++ b/src/bin/cargo.rs @@ -101,39 +101,54 @@ fn execute(flags: Flags, config: &Config) -> CliResult> { return Ok(None) } - let (mut args, command) = match &flags.arg_command[..] { + let args = match &flags.arg_command[..] { + // For the commands `cargo` and `cargo help`, re-execute ourselves as + // `cargo -h` so we can go through the normal process of printing the + // help message. "" | "help" if flags.arg_args.len() == 0 => { config.shell().set_verbose(true); - let args = &["foo".to_string(), "-h".to_string()]; + let args = &["cargo".to_string(), "-h".to_string()]; let r = cargo::call_main_without_stdin(execute, config, USAGE, args, false); - cargo::process_executed(r, &mut **config.shell()); + cargo::process_executed(r, &mut config.shell()); return Ok(None) } + + // For `cargo help -h` and `cargo help --help`, print out the help + // message for `cargo help` "help" if flags.arg_args[0] == "-h" || - flags.arg_args[0] == "--help" => - (flags.arg_args, "help"), - "help" => (vec!["-h".to_string()], &flags.arg_args[0][..]), - s => (flags.arg_args.clone(), s), + flags.arg_args[0] == "--help" => { + vec!["cargo".to_string(), "help".to_string(), "help".to_string()] + } + + // For `cargo help foo`, print out the usage message for the specified + // subcommand by executing the command with the `-h` flag. + "help" => { + vec!["cargo".to_string(), "help".to_string(), + flags.arg_args[0].clone()] + } + + // For all other invocations, we're of the form `cargo foo args...`. We + // use the exact environment arguments to preserve tokens like `--` for + // example. + _ => env::args().collect(), }; - args.insert(0, command.to_string()); - args.insert(0, "foo".to_string()); macro_rules! cmd{ ($name:ident) => ( - if command == stringify!($name).replace("_", "-") { + if args[1] == stringify!($name).replace("_", "-") { mod $name; config.shell().set_verbose(true); let r = cargo::call_main_without_stdin($name::execute, config, $name::USAGE, &args, false); - cargo::process_executed(r, &mut **config.shell()); + cargo::process_executed(r, &mut config.shell()); return Ok(None) } ) } each_subcommand!(cmd); - execute_subcommand(&command, &args, &mut config.shell()); + execute_subcommand(&args[1], &args, &mut config.shell()); Ok(None) } @@ -230,7 +245,7 @@ fn is_executable(path: &Path) -> bool { } #[cfg(windows)] fn is_executable(path: &Path) -> bool { - path.is_file() + fs::metadata(path).map(|m| m.is_file()) == Ok(true) } /// Get `Command` to run given command. diff --git a/tests/support/paths.rs b/tests/support/paths.rs index 8cbd24063..491990c0a 100644 --- a/tests/support/paths.rs +++ b/tests/support/paths.rs @@ -1,7 +1,7 @@ use std::env; use std::fs; use std::io::prelude::*; -use std::io; +use std::io::{self, ErrorKind}; use std::path::{Path, PathBuf}; use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering}; @@ -32,7 +32,29 @@ impl CargoPathExt for Path { */ fn rm_rf(&self) -> io::Result<()> { if self.exists() { - fs::remove_dir_all(self) + for file in fs::read_dir(self).unwrap() { + let file = try!(file).path(); + + if file.is_dir() { + try!(file.rm_rf()); + } else { + // On windows we can't remove a readonly file, and git will + // often clone files as readonly. As a result, we have some + // special logic to remove readonly files on windows. + match fs::remove_file(&file) { + Ok(()) => {} + Err(ref e) if cfg!(windows) && + e.kind() == ErrorKind::PermissionDenied => { + let mut p = file.metadata().unwrap().permissions(); + p.set_readonly(false); + fs::set_permissions(&file, p).unwrap(); + try!(fs::remove_file(&file)); + } + Err(e) => return Err(e) + } + } + } + fs::remove_dir(self) } else { Ok(()) } diff --git a/tests/support/registry.rs b/tests/support/registry.rs index ec8fa0ab3..9cb06dfaa 100644 --- a/tests/support/registry.rs +++ b/tests/support/registry.rs @@ -88,8 +88,8 @@ pub fn mock_pkg_yank(name: &str, version: &str, deps: &[(&str, &str, &str)], let file = match name.len() { 1 => format!("1/{}", name), 2 => format!("2/{}", name), - 3 => format!("3/{}/{}", name.slice_to(1), name), - _ => format!("{}/{}/{}", name.slice(0, 2), name.slice(2, 4), name), + 3 => format!("3/{}/{}", &name[..1], name), + _ => format!("{}/{}/{}", &name[0..2], &name[2..4], name), }; publish(file.as_slice(), line.as_slice()); } diff --git a/tests/test_cargo_build_auth.rs b/tests/test_cargo_build_auth.rs index 1c7705167..eaa495f3e 100644 --- a/tests/test_cargo_build_auth.rs +++ b/tests/test_cargo_build_auth.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; -use std::old_io::net::tcp::TcpAcceptor; -use std::old_io::{TcpListener, Listener, Acceptor, BufferedStream}; +use std::io::BufStream; +use std::io::prelude::*; +use std::net::TcpListener; use std::thread; use git2; @@ -11,23 +12,12 @@ use hamcrest::assert_that; fn setup() { } -struct Closer { a: TcpAcceptor } - -impl Drop for Closer { - fn drop(&mut self) { - let _ = self.a.close_accept(); - } -} - // Test that HTTP auth is offered from `credential.helper` test!(http_auth_offered { - let mut listener = TcpListener::bind("127.0.0.1:0").unwrap(); - let addr = listener.socket_name().unwrap(); - let mut a = listener.listen().unwrap(); - let a2 = a.clone(); - let _c = Closer { a: a2 }; + let a = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = a.socket_addr().unwrap(); - fn headers(rdr: &mut R) -> HashSet { + fn headers(rdr: &mut BufRead) -> HashSet { let valid = ["GET", "Authorization", "Accept", "User-Agent"]; rdr.lines().map(|s| s.unwrap()) .take_while(|s| s.len() > 2) @@ -39,7 +29,7 @@ test!(http_auth_offered { } let t = thread::spawn(move|| { - let mut s = BufferedStream::new(a.accept().unwrap()); + let mut s = BufStream::new(a.accept().unwrap().0); let req = headers(&mut s); s.write_all(b"\ HTTP/1.1 401 Unauthorized\r\n\ @@ -53,7 +43,7 @@ test!(http_auth_offered { ].into_iter().map(|s| s.to_string()).collect()); drop(s); - let mut s = BufferedStream::new(a.accept().unwrap()); + let mut s = BufStream::new(a.accept().unwrap().0); let req = headers(&mut s); s.write_all(b"\ HTTP/1.1 401 Unauthorized\r\n\ @@ -91,7 +81,7 @@ test!(http_auth_offered { script.display().to_string().as_slice()).unwrap(); let p = project("foo") - .file("Cargo.toml", format!(r#" + .file("Cargo.toml", &format!(r#" [project] name = "foo" version = "0.0.1" @@ -99,7 +89,7 @@ test!(http_auth_offered { [dependencies.bar] git = "http://127.0.0.1:{}/foo/bar" - "#, addr.port).as_slice()) + "#, addr.port())) .file("src/main.rs", ""); assert_that(p.cargo_process("build").arg("-v"), @@ -125,17 +115,14 @@ Caused by: // Boy, sure would be nice to have a TLS implementation in rust! test!(https_something_happens { - let mut listener = TcpListener::bind("127.0.0.1:0").unwrap(); - let addr = listener.socket_name().unwrap(); - let mut a = listener.listen().unwrap(); - let a2 = a.clone(); - let _c = Closer { a: a2 }; + let a = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = a.socket_addr().unwrap(); let t = thread::spawn(move|| { drop(a.accept().unwrap()); }); let p = project("foo") - .file("Cargo.toml", format!(r#" + .file("Cargo.toml", &format!(r#" [project] name = "foo" version = "0.0.1" @@ -143,7 +130,7 @@ test!(https_something_happens { [dependencies.bar] git = "https://127.0.0.1:{}/foo/bar" - "#, addr.port).as_slice()) + "#, addr.port())) .file("src/main.rs", ""); assert_that(p.cargo_process("build").arg("-v"), @@ -175,11 +162,8 @@ Caused by: // Boy, sure would be nice to have an SSH implementation in rust! test!(ssh_something_happens { - let mut listener = TcpListener::bind("127.0.0.1:0").unwrap(); - let addr = listener.socket_name().unwrap(); - let mut a = listener.listen().unwrap(); - let a2 = a.clone(); - let _c = Closer { a: a2 }; + let a = TcpListener::bind("127.0.0.1:0").unwrap(); + let addr = a.socket_addr().unwrap(); let t = thread::spawn(move|| { drop(a.accept().unwrap()); }); @@ -193,7 +177,7 @@ test!(ssh_something_happens { [dependencies.bar] git = "ssh://127.0.0.1:{}/foo/bar" - "#, addr.port).as_slice()) + "#, addr.port()).as_slice()) .file("src/main.rs", ""); assert_that(p.cargo_process("build").arg("-v"), diff --git a/tests/test_cargo_compile.rs b/tests/test_cargo_compile.rs index 3fb658dbb..253f81812 100644 --- a/tests/test_cargo_compile.rs +++ b/tests/test_cargo_compile.rs @@ -1,6 +1,7 @@ use std::env; -use std::fs::{self, TempDir, File}; +use std::fs::{self, File}; use std::io::prelude::*; +use tempdir::TempDir; use support::{project, execs, main_file, basic_bin_manifest}; use support::{COMPILING, RUNNING, ProjectBuilder}; diff --git a/tests/test_cargo_cross_compile.rs b/tests/test_cargo_cross_compile.rs index fcb677558..aaf38bfb9 100644 --- a/tests/test_cargo_cross_compile.rs +++ b/tests/test_cargo_cross_compile.rs @@ -142,7 +142,7 @@ test!(plugin_deps { use rustc::plugin::Registry; use syntax::ast::TokenTree; use syntax::codemap::Span; - use syntax::ext::base::{ExtCtxt, MacExpr, MacResult}; + use syntax::ext::base::{ExtCtxt, MacEager, MacResult}; #[plugin_registrar] pub fn foo(reg: &mut Registry) { @@ -151,7 +151,7 @@ test!(plugin_deps { fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box { - MacExpr::new(quote_expr!(cx, 1)) + MacEager::expr(quote_expr!(cx, 1)) } "#); let baz = project("baz") @@ -222,7 +222,7 @@ test!(plugin_to_the_max { use rustc::plugin::Registry; use syntax::ast::TokenTree; use syntax::codemap::Span; - use syntax::ext::base::{ExtCtxt, MacExpr, MacResult}; + use syntax::ext::base::{ExtCtxt, MacEager, MacResult}; #[plugin_registrar] pub fn foo(reg: &mut Registry) { @@ -231,7 +231,7 @@ test!(plugin_to_the_max { fn expand_bar(cx: &mut ExtCtxt, sp: Span, tts: &[TokenTree]) -> Box { - MacExpr::new(quote_expr!(cx, baz::baz())) + MacEager::expr(quote_expr!(cx, baz::baz())) } "#); let baz = project("baz") diff --git a/tests/test_cargo_new.rs b/tests/test_cargo_new.rs index ef873526a..d2bfd438d 100644 --- a/tests/test_cargo_new.rs +++ b/tests/test_cargo_new.rs @@ -1,6 +1,7 @@ -use std::fs::{self, File, TempDir}; +use std::fs::{self, File}; use std::io::prelude::*; use std::env; +use tempdir::TempDir; use support::{execs, paths, cargo_dir}; use hamcrest::{assert_that, existing_file, existing_dir, is_not}; diff --git a/tests/test_cargo_registry.rs b/tests/test_cargo_registry.rs index 730b31414..47ae6bd25 100644 --- a/tests/test_cargo_registry.rs +++ b/tests/test_cargo_registry.rs @@ -441,7 +441,7 @@ test!(update_with_lockfile_if_packages_missing { execs().with_status(0)); p.root().move_into_the_past().unwrap(); - fs::remove_dir_all(&paths::home().join(".cargo/registry")).unwrap(); + paths::home().join(".cargo/registry").rm_rf().unwrap(); assert_that(p.cargo("build"), execs().with_status(0).with_stdout(format!("\ {updating} registry `[..]` @@ -470,7 +470,7 @@ test!(update_lockfile { r::mock_pkg("bar", "0.0.2", &[]); r::mock_pkg("bar", "0.0.3", &[]); - fs::remove_dir_all(&paths::home().join(".cargo/registry")).unwrap(); + paths::home().join(".cargo/registry").rm_rf().unwrap(); println!("0.0.2 update"); assert_that(p.cargo("update") .arg("-p").arg("bar").arg("--precise").arg("0.0.2"), diff --git a/tests/tests.rs b/tests/tests.rs index d526e9751..356690d86 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -1,5 +1,5 @@ -#![feature(core, io, old_io, os, old_path)] -#![feature(std_misc, io, path, fs, tempdir)] +#![feature(core, io, old_io, old_path)] +#![feature(std_misc, io, path, fs, net, path_ext, fs_time, fs_walk)] extern crate "rustc-serialize" as serialize; extern crate cargo; @@ -9,6 +9,7 @@ extern crate hamcrest; extern crate tar; extern crate term; extern crate url; +extern crate tempdir; #[macro_use] extern crate log; -- 2.30.2